//
//  LogViewController.m
//  Manage
//
//  Created by 杨康 on 2017/10/30.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "LogViewController.h"
#import "ForgetPasswordViewController.h"
#import "UserModel.h"
@interface LogViewController ()
{
    YKTextField *_phoneTF;
    YKTextField *_passTF;
}
@end

@implementation LogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorF];
    self.navigationItem.title = @"登录";
    
    
    
    
    
    
    
    
    
    //手机号
    YKTextField *phoneTF=[SingLeton myTextFieldsecure:NO andkeyboardType:UIKeyboardTypeNumberPad andplaceholder:@"请输入手机号" andLeftImage:nil RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
        
    }];
    [self.view addSubview:phoneTF];
    _phoneTF=phoneTF;
    [phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30+TOP_HEIGHT);
        make.left.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, 44));
    }];
    
    //密码
    YKTextField *passTF=[SingLeton myTextFieldsecure:YES andkeyboardType:UIKeyboardTypeDefault andplaceholder:@"请输入登录密码" andLeftImage:nil RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
        
    }];
    [self.view addSubview:passTF];
    _passTF=passTF;
    [passTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(phoneTF.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, 44));
    }];
    
    //登录按钮
    UIButton  *logButton=[SingLeton endButton:[UIColor colorQ] andTitle:@"登录" RequestSuccess:^(UIButton *btn) {
        [UDF removeObjectForKey:@"token"];
        
        NSString *URLString = @"/axq/admin/login";
        NSDictionary *parameters = @{@"loginid": _phoneTF.text, @"password": _passTF.text};
        [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
            LRLog(@"%@",responseObject);
            NSString *code = responseObject[@"code"];
            if([code isEqualToString:@"10005"]){
                NSDictionary *data=responseObject[@"data"];
                UserModel *model=[UserModel new];
                model.addtime=[NSString stringWithFormat:@"%@",data[@"addtime"]];
                model.updatetime=[NSString stringWithFormat:@"%@",data[@"updatetime"]];
                model.lat=[NSString stringWithFormat:@"%@",data[@"lat"]];
                model.lon=[NSString stringWithFormat:@"%@",data[@"lon"]];
                model.password=[NSString stringWithFormat:@"%@",data[@"password"]];
                model.telephone=[NSString stringWithFormat:@"%@",data[@"telephone"]];
                model.userCode=[NSString stringWithFormat:@"%@",data[@"userCode"]];
                model.userRole=[NSString stringWithFormat:@"%@",data[@"userRole"]];
                model.userTitle=[NSString stringWithFormat:@"%@",data[@"userTitle"]];
                model.address=[NSString stringWithFormat:@"%@",data[@"address"]];
                model.contacts=[NSString stringWithFormat:@"%@",data[@"contacts"]];
                model.headimg=[NSString stringWithFormat:@"%@",data[@"headimg"]];
                model.someId=[NSString stringWithFormat:@"%@",data[@"id"]];
                model.linkman=[NSString stringWithFormat:@"%@",data[@"linkman"]];
                model.loginId=[NSString stringWithFormat:@"%@",data[@"loginId"]];
                model.status=[NSString stringWithFormat:@"%@",data[@"status"]];
                
                [UDF setObject:model.userRole forKey:@"token"];
                AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                [delegate switchController];
                
            }else{
                NSString *str=responseObject[@"msg"];
                
                [self.view makeToast:str];
            }
            
            
        } RequestFailed:^(NSError *error) {
            
        }];
        
        
        
    }];
    [self.view addSubview:logButton];
    [logButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passTF.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(passTF);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, 44));
    }];
    
    //忘记密码
    UIButton * forgetButton=[SingLeton endButton:nil andTitle:@"忘记密码" RequestSuccess:^(UIButton *btn) {
        ForgetPasswordViewController *vc=[[ForgetPasswordViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [forgetButton setTitleColor:[UIColor colorQ] forState:UIControlStateNormal];
    [self.view addSubview:forgetButton];
    [forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(logButton.mas_bottom).mas_offset(35);
        make.centerX.mas_equalTo(logButton);
        
    }];
    
//    NSArray *logArr=[NSArray arrayWithObjects:@"门店登录",@"修理商登录",@"电动车厂商登录",@"民警登录",@"物业登录", nil];
//    for (int i=0; i<5; i++) {
//
//        int a=i/2;
//        int b=i%2;
//        CGFloat width=(SCREEN_WIDTH-60)/2;
//        //登录按钮
//        UIButton  *logButton=[SingLeton endButton:[UIColor colorQ] andTitle:logArr[i] RequestSuccess:^(UIButton *btn) {
//
//
//            if ([btn.titleLabel.text isEqualToString:@"修理商登录"]) {
//                [self.view makeToast:@"开发中"];
//                return ;
//            }else
//            {
//                NSString *URLString = @"/axq/admin/login";
//                NSDictionary *parameters = @{@"loginid": _phoneTF.text, @"password": _passTF.text};
//                [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
//
//
//                } RequestFailed:^(NSError *error) {
//
//                }];
//
//            }
//
//        }];
//        [self.view addSubview:logButton];
//        [logButton mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.mas_equalTo(forgetButton.mas_bottom).mas_offset(30+(44+15)*a);
//            make.left.mas_equalTo(20+(width+20)*b);
//            make.size.mas_equalTo(CGSizeMake(width, 44));
//        }];
//    }
    
    
    
    
    
}





@end
